﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>屏幕键盘 - 脚本示例 | AutoHotkey v2</title>
<meta name="description" content="The on-screen keyboard script creates a virtual keyboard at the bottom of your screen that shows the keys you are pressing in real time." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>屏幕键盘(需要 XP/2k/NT) <span class="headnote">作者: Jon(基于 v1 脚本)</span></h1>

<p>此脚本会在屏幕的底部创建一个模拟键盘来实时显示您按下的按键. 我写这个是为了帮我学习盲打(来适应不去看键盘). 在脚本的顶部可以自定义屏幕键盘的大小. 还有, 您可以双击托盘图标来显示或隐藏键盘.
</p>
<p><a href="KeyboardOnScreen.ahk">下载此脚本</a> &nbsp;| &nbsp;<a href="index.htm">其他示例脚本</a> &nbsp;| &nbsp;<a href="../AutoHotkey.htm">主页</a></p>

<pre class="NoIndent"><em>;---- Configuration Section: Customize the size of the on-screen keyboard and
; other options here.</em>

<em>; Changing this font size will make the entire on-screen keyboard get
; larger or smaller:</em>
k_FontSize := 10
k_FontName := "Verdana"  <em>; This can be blank to use the system's default font.</em>
k_FontStyle := "Bold"    <em>; 示例 of an alternative: Italic Underline</em>

<em>; Names for the tray menu items:</em>
k_MenuItemHide := "Hide on-screen &amp;keyboard"
k_MenuItemShow := "Show on-screen &amp;keyboard"

<em>; To have the keyboard appear on a monitor other than the primary, specify
; a number such as 2 for the following variable. Leave it blank to use
; the primary:</em>
k_Monitor := ""

<em>;---- End of configuration section. Don't change anything below this point
; unless you want to alter the basic nature of the script.</em>

<em>;---- Create a GUI window for the on-screen keyboard:</em>
Gui := GuiCreate("-Caption +ToolWindow +AlwaysOnTop +Disabled")
Gui.SetFont("s" k_FontSize " " k_FontStyle, k_FontName)
Gui.MarginY := 0, Gui.MarginX := 0

<em>;---- Alter the tray icon menu:</em>
A_TrayMenu.Delete
A_TrayMenu.Add k_MenuItemHide, (*) =&gt; k_ShowHide(Gui, k_MenuItemHide, k_MenuItemShow)
A_TrayMenu.Add "&amp;Exit", (*) =&gt; ExitApp()
A_TrayMenu.Default := k_MenuItemHide

<em>;---- Add a button for each key:</em>

<em>; The keyboard layout:</em>
k_Layout := [
    ["``", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "Backspace:3"],
    ["Tab:3", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\"],
    ["CapsLock:3", "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "Enter:2"],
    ["LShift:3", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/", "Shift:3"],
    ["LCtrl:2", "LWin:2", "LAlt:2", "Space:2", "RAlt:2", "RWin:2", "AppsKey:2", "RCtrl:2"]
]

<em>; Traverse the keys of the keyboard layout:</em>
for n, k_Row in k_Layout
    for i, k_Key in k_Row
    {
        k_KeyWidthMultiplier := 1
        <em>; Get custom key width multiplier:</em>
        if RegExMatch(k_Key, "(.+):(\d)", m)
        {
            k_Key := m[1]
            k_KeyWidthMultiplier := m[2]
        }
        <em>; Get localized key name:</em>
        k_KeyNameText := GetKeyNameText(k_Key, 0, 1)
        <em>; Windows key names start with left or right so replace it:</em>
        if (k_Key = "LWin" || k_Key = "RWin")
            k_KeyNameText := "Win"
         <em>; Truncate the key name:</em>
        if (StrLen(k_Key) &gt; 1)
            k_KeyNameText := Trim(SubStr(k_KeyNameText, 1, 5))
        else
            k_KeyNameText := k_Key
        <em>; Convert to uppercase:</em>
        k_KeyNameText := StrUpper(k_KeyNameText)
        <em>; Calculate object dimensions based on chosen font size:</em>
        k_KeyHeight := k_FontSize * 3
        opt := "h" k_KeyHeight " w" k_KeyHeight * k_KeyWidthMultiplier " -Wrap x+m" 
        if (i = 1)
            opt .= " y+m xm"
        <em>; Add the button:</em>
        Btn := Gui.Add("Button", opt, k_KeyNameText)
        <em>; When a key is pressed by the user, click the corresponding button on-screen:</em>
        Hotkey("~*" k_Key, Func("k_KeyPress").bind(Btn))
    }

<em>;---- Position the keyboard at the bottom of the screen (taking into account
; the position of the taskbar):</em>
Gui.Show("Hide") <em>; Required to get the window's calculated width and height.
; Calculate window's X-position:</em>
MonitorGetWorkArea(k_Monitor, WL,, WR, WB)
k_xPos := (WR - WL - Gui.Pos.W) / 2 <em>; Calculate position to center it horizontally.
; The following is done in case the window will be on a non-primary monitor
; or if the taskbar is anchored on the left side of the screen:</em>
k_xPos += WL
<em>; Calculate window's Y-position:</em>
k_yPos := WB - Gui.Pos.H

<em>;---- Show the window:</em>
Gui.Show("x" k_xPos " y" k_yPos " NA")

<em>;---- Function definitions:</em>
k_KeyPress(BtnCtrl)
{ 
    BtnCtrl.Opt("Default") <em>; Highlight the last pressed key.</em>
    ControlClick(, "ahk_id " BtnCtrl.Hwnd,,,, "D")
    KeyWait(SubStr(A_ThisHotkey, 3))
    ControlClick(, "ahk_id " BtnCtrl.Hwnd,,,, "U")
}

k_ShowHide(GuiObj, HideText, ShowText)
{
    static isVisible := true
    if isVisible
    {
        GuiObj.Hide
        A_TrayMenu.Rename HideText, ShowText
        isVisible := false
    }
    else
    {
        GuiObj.Show
        A_TrayMenu.Rename ShowText, HideText
        isVisible := true
    }
}

GetKeyNameText(Key, Extended := false, DoNotCare := false)
{
    Params := (GetKeySC(Key) &lt;&lt; 16) | (Extended &lt;&lt; 24) | (DoNotCare &lt;&lt; 25)
    KeyNameText := BufferAlloc(64, 0)
    DllCall("User32.dll\GetKeyNameText", "Int", Params, "Ptr", KeyNameText, "Int", 32)
    return StrGet(KeyNameText)
}</pre>
</body>
</html>